/**
 * 2011-11-15上午10:13:26
 */
package dao;

import java.sql.ResultSet;
import java.sql.SQLException;

import util.MysqlConn;
import bean.User;

/**
 * @author Baicai 查询一个用户
 */
public class SelectOneUser {

	/**
	 * 根据学号查询id
	 * 
	 * @param string
	 * @return int
	 */
	public static int getUserId(String string) {

		String sqlString = "select user_id from user where userinfo_num='"
				+ string + "'";// 拼接sql字符串，查询指定学号的信息
		MysqlConn.setSqlString(sqlString);// 设置静态方法中的变量
		ResultSet resultSet;

		try {
			resultSet = MysqlConn.queryDB();
			resultSet.next();
			int id = resultSet.getInt(1);
			return id;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}// 以resultSet获取查询结果
		finally {
			MysqlConn.free();
		}
		return 0;
	}

	/**
	 * 根据num查询一个用户的信息
	 * 
	 * @param string
	 * @return User
	 */
	public static User getOneUser(String string) {

		try {

			User user = new User();
			MysqlConn.setSqlString("select * from user where userinfo_num='"
					+ string + "'");// 设置sql语句
			ResultSet resultSet = MysqlConn.queryDB();// 执行，或取结果集
			resultSet.next();
			user.setUserId(resultSet.getInt(1));// 将结果放入user
			user.setUserinfoName(resultSet.getString("userinfo_name"));
			user.setUserinfoNum(resultSet.getString("userinfo_num"));
			user.setUserinfoSex(resultSet.getString("userinfo_sex"));
			user.setUserScore(Integer.parseInt(resultSet
					.getString("user_score")));
			user.setUserPhone(resultSet.getString("user_phone"));
			user.setUserInformation(resultSet.getString("user_information"));

			return user;

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			MysqlConn.free();
		}

		return null;
	}

	/**
	 * 根据id查询一个用户的信息
	 * 
	 * @param i
	 * @return User
	 */
	public static User getOneUser(int i) {

		try {

			User user = new User();
			MysqlConn.setSqlString("select * from user where user_id='" + i
					+ "'");// 设置sql语句
			ResultSet resultSet = MysqlConn.queryDB();// 执行，或取结果集
			resultSet.next();
			user.setUserId(i);// 将结果放入user
			user.setUserinfoName(resultSet.getString("userinfo_name"));
			user.setUserinfoNum(resultSet.getString("userinfo_num"));
			user.setUserinfoSex(resultSet.getString("userinfo_sex"));
			user.setUserScore(Integer.parseInt(resultSet
					.getString("user_score")));
			user.setUserPhone(resultSet.getString("user_phone"));
			user.setUserInformation(resultSet.getString("user_information"));

			return user;

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			MysqlConn.free();
		}

		return null;
	}
}
